from zope.component import queryMultiAdapter
from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile
from plone.app.i18n.locales.browser.selector import LanguageSelector
from plone.app.multilingual.interfaces import ITranslationGroupLookup, NotTranslatable


class TranslatableLanguageSelector(LanguageSelector):
    """Language selector for translatable content.
    """

    render = ZopeTwoPageTemplateFile('languageselector.pt')

    def languages(self):
        results = LanguageSelector.languages(self)
        translations = []
        
        try:
            translations = ITranslationGroupLookup(self.context).getTranslationLanguages()
        except NotTranslatable:
            return results
        except TypeError:
            return results

        #for data in translations:
            #data['translated'] = data['code'] in translations
            #if data['translated']:
            #    trans = translations[data['code']][0]
            #    state = queryMultiAdapter((trans, self.request),
            #            name='plone_context_state')
            #    data['url'] = state.view_url() + '?set_language=' + data['code']
            #else:
            #    state = queryMultiAdapter((self.context, self.request),
            #            name='plone_context_state')
            #    data['url'] = state.view_url() + '?set_language=' + data['code']
            
        return results


